package com.uprr.netcontrol.starmap.rest.service;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.xml.bind.JAXBElement;

import org.springframework.stereotype.Component;

import com.up.www.schemas.netcontrol.common.starmap.nc_du_deployment_descriptor_1_0.DeploymentUnit;
import com.uprr.netcontrol.starmap.dto.DeploymentUnitDTO;
import com.uprr.netcontrol.starmap.persistence.DeploymentUnitDao;
import com.uprr.netcontrol.starmap.rest.processor.DataProcessor;

@Component
@Path("/deployment-unit")
public class DeploymentHandler {

  private DataProcessor processor;
  private DeploymentUnitDao graphDataDao;

  @POST
  @Path("/save")
  @Consumes("application/xml")
  public Response saveDeploymentDescriptor(final JAXBElement<DeploymentUnit> deploymentUnitXml) {
    if (null != deploymentUnitXml && null != deploymentUnitXml.getValue()) {
      DeploymentUnitDTO rootDto = processor.process(deploymentUnitXml.getValue());
      graphDataDao.save(rootDto);
    }
    String result = "success";
    return Response.status(Status.OK).entity(result).build();
  }

  public final DataProcessor getProcessor() {
    return processor;
  }

  public final void setProcessor(final DataProcessor processor) {
    this.processor = processor;
  }

  public final DeploymentUnitDao getGraphDataDao() {
    return graphDataDao;
  }

  public final void setGraphDataDao(final DeploymentUnitDao graphDataDao) {
    this.graphDataDao = graphDataDao;
  }

}
